﻿<xsl:stylesheet version="1.0"
	xmlns:date="http://exslt.org/dates-and-times"
  xmlns:ebe="http://cks/ebemethods"
	xmlns:string="http://exslt.org/strings"
	xmlns:ms="urn:schemas-microsoft-com:xslt"
  xmlns:dt="urn:schemas-microsoft-com:datatypes"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="date ebe string">

  <xsl:param name="IsBlogOwner" />
  <xsl:param name="RelativeUrl" />
  <xsl:param name="QSpageindex" />
  <xsl:param name="TrimPostBodyOnLists"/>
  <xsl:param name="Language" />
  <xsl:param name="ShortDatePattern" />
  <xsl:param name="ShortTimePattern" />
  <xsl:param name="FullDateTimePattern" />
  <xsl:param name="NoPostInCategory" />

	<xsl:output omit-xml-declaration="yes" />

	<xsl:template match="/">

		<xsl:variable name="endPos" select="number($QSpageindex) * 10"/>

		<xsl:choose>
			<!-- Is there a Query String for the page index -->
			<xsl:when test="$QSpageindex">
				<xsl:apply-templates select="rows/row[position() &gt; number($endPos)-10 and position() &lt; number($endPos)+1]"/>
			</xsl:when>
			<!-- Nope...just show the first 10 -->
			<xsl:otherwise>
				<xsl:apply-templates select="rows/row[position() &lt; 11]"/>
			</xsl:otherwise>
		</xsl:choose>

		<xsl:variable name="pos">
			<xsl:choose>
				<xsl:when test="not($QSpageindex)">1</xsl:when>
				<xsl:otherwise><xsl:value-of select="number($QSpageindex)"/></xsl:otherwise>
			</xsl:choose>
			
		</xsl:variable>
		
		<xsl:if test="number($endPos) &gt; 10">
			<!-- We are on another page...show the previous -->
			<a>
				<xsl:attribute name="href">
					?PageIndex=<xsl:value-of select="number($pos)-1"/>
				</xsl:attribute>
				&lt;&lt; <xsl:value-of select="ebe:res('Previous')"/> &#160;
			</a>
		</xsl:if>

		<xsl:if test="(not($QSpageindex) and count(rows/row) &gt; 10) or (($endPos) and number($endPos) &lt; count(rows/row))">
			<!-- There appear to be more posts...show a 'next' link -->
			<a>
				<xsl:attribute name="href">?PageIndex=<xsl:value-of select="number($pos)+1"/></xsl:attribute>
				&#160;<xsl:value-of select="ebe:res('Next')"/> &gt;&gt;
			</a>
		</xsl:if>

	</xsl:template>

	<xsl:template match="row[position()=1]">

		<h2>
			<b>
				<a title="{Title}">
					<xsl:attribute name="href">
						<xsl:value-of select="ebe:createPostUrl(PublishedDate, BlogTitleForUrl)"/>
					</xsl:attribute>
					<xsl:value-of select="Title"/>
				</a>
			</b>
		</h2>
		
		<xsl:call-template name="Body"/>
		
	</xsl:template>		

	<xsl:template match="row">

		<h3>
			<b>
				<a title="{Title}">
					<xsl:attribute name="href">
						<xsl:value-of select="ebe:createPostUrl(PublishedDate, BlogTitleForUrl)"/>
					</xsl:attribute>
					<xsl:value-of select="Title"/>
				</a>
			</b>
		</h3>
		
		<xsl:call-template name="Body"/>
		
	</xsl:template>		

	<xsl:template name="Body">
		<xsl:variable name="publishedDate" select="date:parseDate(PublishedDate, 'yyyy-MM-dd HH:mm:ss')"/>

		<h4 class="date">
      <xsl:value-of select="ms:format-date($publishedDate, $FullDateTimePattern, $Language)"/> - <xsl:value-of select="ms:format-time($publishedDate, $ShortTimePattern, $Language)"/>&#160;
      <xsl:value-of select="ebe:res('ShortPostedBy')"/>&#160;
			<xsl:value-of select="substring-after(Author,'#')"/>
		</h4>

		<xsl:variable name="pureText">
			<xsl:call-template name="removeHtmlTags">
				<xsl:with-param name="html" select="Body" />
			</xsl:call-template>
		</xsl:variable>

		<p>
			<xsl:choose>
				<xsl:when test="$TrimPostBodyOnLists = 'True'" ><xsl:value-of select="substring($pureText, 0, 250)" disable-output-escaping="yes" />...</xsl:when>
				<xsl:otherwise><xsl:value-of select="Body" disable-output-escaping="yes"/></xsl:otherwise>
			</xsl:choose>
			
		</p>

		<h4>
				<a title="{Title} - Comments">
					<xsl:attribute name="href">
						<xsl:value-of select="ebe:createPostUrl(PublishedDate, BlogTitleForUrl)"/>#postComments
					</xsl:attribute>
          <xsl:value-of select="NumComments"/>&#160;
          <xsl:choose>
            <xsl:when test="NumComments = 1">
              <xsl:value-of select="ebe:res('Comment')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="ebe:res('Comments')"/>
            </xsl:otherwise>
          </xsl:choose>
        </a>
				<xsl:if test="Categories != ''">
					 - <xsl:for-each select="string:split(Categories, ';')">
					 	<a href="{ebe:createTagUrl(.)}">
						 	<xsl:value-of select="."/>
					 	</a>
					 	<xsl:if test="position() != last()">, </xsl:if>
					 </xsl:for-each>
				</xsl:if>
		</h4>
		

		<xsl:if test="$IsBlogOwner = 'True'">
			<div class="blogCommands small">
				<a>
					<xsl:attribute name="href">
						/<xsl:value-of select="substring-after(FileDirRef,'#')"/>/EditPost.aspx?ID=<xsl:value-of select="ID"/>
					</xsl:attribute>
					[<xsl:value-of select="ebe:res('EditPost')"/>]
				</a>
			</div>
		</xsl:if>

		<hr/>
	</xsl:template>

	<xsl:template name="removeHtmlTags">
		<xsl:param name="html"/>
		<xsl:choose>
			<xsl:when test="contains($html, '&lt;')">
				<xsl:value-of select="substring-before($html, '&lt;')"/>
				<xsl:call-template name="removeHtmlTags">
					<!-- Recurse through HTML -->
					<xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$html" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>
